package com.zishi.venuesbooking.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zishi.venuesbooking.R;
import com.zishi.venuesbooking.entity.Comment;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import de.hdodenhof.circleimageview.CircleImageView;

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentViewHolder> {

    private List<Comment> comments;

    public CommentAdapter(List<Comment> comments) {
        this.comments = comments;
    }

    @Override
    public CommentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_comment, parent, false);
        return new CommentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(CommentViewHolder holder, int position) {
        Comment comment = comments.get(position);
        holder.bind(comment);
    }

    @Override
    public int getItemCount() {
        return comments.size();
    }

    // 更新数据
    public void updateData(List<Comment> newComments) {
        comments.clear();
        comments.addAll(newComments);
        notifyDataSetChanged();
    }

    // 添加新评论
    public void addComment(Comment comment) {
        comments.add(0, comment); // 添加到列表顶部
        notifyItemInserted(0);
    }

    static class CommentViewHolder extends RecyclerView.ViewHolder {
        private CircleImageView civAvatar;
        private TextView tvVenueName;
        private ImageView[] stars = new ImageView[5];
        private TextView tvRating;
        private TextView tvComment;
        private TextView tvTime;

        public CommentViewHolder(View itemView) {
            super(itemView);
            civAvatar = itemView.findViewById(R.id.civ_avatar);
            tvVenueName = itemView.findViewById(R.id.tv_venue_name);
            stars[0] = itemView.findViewById(R.id.iv_star1);
            stars[1] = itemView.findViewById(R.id.iv_star1);
            stars[2] = itemView.findViewById(R.id.iv_star1);
            stars[3] = itemView.findViewById(R.id.iv_star1);
            stars[4] = itemView.findViewById(R.id.iv_star1);
            tvRating = itemView.findViewById(R.id.tv_rating);
            tvComment = itemView.findViewById(R.id.tv_comment);
            tvTime = itemView.findViewById(R.id.tv_time);
        }

        public void bind(Comment comment) {
            // 加载用户头像
            Glide.with(itemView.getContext())
                    .load(comment.getUserAvatar())
                    .placeholder(R.drawable.avata_1)
                    .into(civAvatar);

            tvVenueName.setText(comment.getVenueName());
            tvComment.setText(comment.getContent());
            tvTime.setText(formatTime(comment.getCreateTime()));
            tvRating.setText(String.valueOf(comment.getRating()));

            // 设置评分星星
            int rating = comment.getRating();
            for (int i = 0; i < stars.length; i++) {
                stars[i].setImageResource(i < rating ?
                        R.drawable.ic_star_filled :
                        R.drawable.ic_star_outline);
            }
        }

        private String formatTime(long time) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
            return sdf.format(new Date(time));
        }
    }
}
